package cn.dlc.com.config;

import java.net.InetAddress;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import cn.dlc.com.property.ZookeeperProperty;

@Configuration
@EnableConfigurationProperties(ZookeeperProperty.class)
public class ProviderAppConfig {
	
	@Autowired
	private Environment env;
	// 获取服务端口
	@Bean
	public ServerProvider serverProvider(ZookeeperProperty zk) throws Exception{
		// 初始化一个服务对象
		ServerProvider sp = new ServerProvider(zk.getUrl(),zk.getNodePath());
		// 获取本机地址
		InetAddress address = InetAddress.getLocalHost();
		System.out.println("本机ip：" + address.getHostAddress());
		// 获取端口
		System.out.println("本机port：" + env.getProperty("server.port"));
		// 作为访问的地址
		String serverUrl = address.getHostAddress() + ":" + env.getProperty("server.port");
		// 注册
		sp.registServer(zk.getNodePath(), zk.getServerName(), serverUrl);
		
		return sp;
	}
}
